import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);


//定义路由
const routes= [
  {  // 前台首页
    path: '/',
    component: () => import('../views/Index'),
    children: [
      { // 路由重定向
        path: '',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: () => import('../views/Home'),
      },
      {  //文章列表页
        path: '/list',
        name: 'list',
        component: () => import('../views/ArticleList')
      },
      {  //文章详情页
        path: '/detail/:id',
        name: 'detail',
        component: () => import('../views/ArticleDetail')
      }
    ]
  },

  {  //登录页
    path: '/login',
    name: 'login',
    component: () => import('../views/Login')
  },

  {  // 后台首页
    path: '/admin',
    meta: {title: '首页'},
    component: () => import('../views/admin/Index'),
    children: [
      {
        path: '',
        redirect: '/admin/home'
      },
      {
        path: '/admin/home',  //个人中心模块
        name: 'admin_home',
        meta: {title: '个人中心'},
        component: () => import('../views/admin/Home'),
      },
      {
        path: '/admin/add_article',  //发表博文模块
        name: 'add_article',
        meta: {title: '发表博文'},
        component: () => import('../views/admin/AddArticle'),
      },
      {
        path: '/admin/article_category',  //文章类别模块
        name: 'article_category',
        meta: {title: '文章类别'},
        component: () => import('../views/admin/ArticleCategory'),
      },
      {
        path: '/admin/article_list',  //文章列表模块
        name: 'article_list',
        meta: {title: '文章列表'},
        component: () => import('../views/admin/ArticleList'),
      },
      {
        path: '/admin/log',  //日志管理模块
        name: 'log',
        meta: {title: '日志管理'},
        component: () => import('../views/admin/Log'),
      },
      {
        path: '/admin/account',  //账户管理模块
        name: 'account',
        meta: {title: '账户管理'},
        component: () => import('../views/admin/Account'),
      }
    ]
  },

];

//创建 router 实例，然后传 `routes` 配置
const router = new VueRouter({
  routes
});

/*//路由拦截  在跳转前执行
router.beforeEach(function(to, from, next){
  if(!sessionStorage.getItem('username')){
    if(to.path!=='/login'){
      next('/login');
    }
  }
  next();
})

// 挂载路由导航守卫来控制访问的权限
// 如果用户没有登陆, 但是直接通过url访问特定页面, 需要重新导航到登陆页面
// 为路由对象, 添加beforeEach导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径    from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行    next() 放行    next('/login') 强制跳转
  // 如果用户访问的是登录页, 直接放行
  if (to.path === '/login') return next()
  // 从sessionStorage中获取保存的token值
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})*/


export default router
